package com.changgou.pay.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.pay.config.RabbitMQConfig;
import com.changgou.pay.service.WXPayService;
import com.changgou.util.ConvertUtils;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author LQQ
 * @Date 2020/3/15 16:45
 * @Version 1.0
 */
@RequestMapping("/wxpay")
@RestController
public class WXPayController {
    @Autowired
    private WXPayService wxPayService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //支付
    @GetMapping("/nativePay")
    public Result nativePay(@RequestParam("orderId") String orderId, @RequestParam("money") Integer money) {

        Map resultMap = wxPayService.nativePay(orderId, money);

        return new Result(true, StatusCode.OK, "支付成功", resultMap);
    }


    @RequestMapping("/notify")
    //当前方法只有在微信支付成功后才能进行回调
    public void notifyLogic(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("支付成功回调");

        try {
            //输入流转换为字符串,微信返回给商户的
            String xml = ConvertUtils.convertToString(request.getInputStream());
            System.out.println(xml);

            //基于微信发送的通知内容,完成后续的业务逻辑处理
            Map<String, String> map = WXPayUtil.xmlToMap(xml);//微信给我们返回的内容

            if ("SUCCESS".equals(map.get("result_code"))) {//判断操作是否成功
                //查询订单
                Map result = wxPayService.queryOrder(map.get("out_trade_no"));

                System.out.println("查询订单结果 = " + result);

                if ("SUCCESS".equals(result.get("result_code"))) {

                    //将订单的消息发送到mq,发送订单id和交易id
                    Map message = new HashMap();

                    message.put("orderId", result.get("out_trade_no"));

                    message.put("transactionId", result.get("transaction_id"));//交易id

                    //消息发送
                    rabbitTemplate.convertAndSend("", RabbitMQConfig.ORDER_PAY, JSON.toJSONString(message));


                    //完成双向通信.支付页面的跳转,支付成功,向这个交换机发消息
                    rabbitTemplate.convertAndSend("paynotify", "", result.get("out_trade_no"));

                } else {
                    //输出错误原因
                    System.out.println(map.get("err_code_des"));
                }

            } else {
                //输出错误原因
                System.out.println(map.get("err_code_des"));
            }

            //给微信一个结果通知
            response.setContentType("text/xml");

            String data = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";

            response.getWriter().write(data);
        } catch (Exception e) {
            e.printStackTrace();


        }
    }


    /**
     * 基于微信查询订单
     *
     * @return
     */
    @GetMapping("/query/{orderId}")
    public Result queryOrder(@PathVariable("orderId") String orderId) {

        Map map = wxPayService.queryOrder(orderId);

        return new Result(true, StatusCode.OK, "查询订单成功", map);

    }


    /**
     * 基于微信关闭订单
     * @param orderId
     * @return
     */
    @PutMapping("/close/{orderId}")
    public Result closeOrder(@PathVariable("orderId") String orderId) {

        Map map = wxPayService.closeOrder(orderId);

        return new Result(true, StatusCode.OK, "关闭订单成功", map);


    }
}
